package com.ita.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ita.pojo.Merchant;
import com.ita.redis.MerchantBlockFactory;

public class CheckBlockFilter implements Filter {

	public void destroy() {
		// TODO Auto-generated method stub
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse resp = (HttpServletResponse) response;
		System.out.println(req.getRequestURI());
		HttpSession session = req.getSession();

		Merchant merchant = (Merchant) session.getAttribute("m");
		String id = merchant.getId();
		int block = MerchantBlockFactory.getBlock(id);

		if (block == 0) {
			chain.doFilter(request, response);
		} else {
			System.out.println(req.getRequestURI()+": was filter by block");
			resp.sendRedirect(req.getContextPath() + "/block.jsp");
		}

	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
